package com.dikang.keer.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.dikang.common.utils.PageUtils;
import com.dikang.common.utils.R;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.dikang.keer.product.entity.CategoryEntity;
import com.dikang.keer.product.service.CategoryService;


/**
 * 商品三级分类
 *
 * @author stbz
 * @email 1287796639@qq.com
 * @date 2021-10-21 02:01:46
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")

    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")

    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateCategory(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds){
        boolean b = categoryService.removeByIds(Arrays.asList(catIds));
        System.out.println(b);
        for (Long catId : catIds) {
            System.out.println(catId);
        }
        if (b==true){
            return R.ok();
        }else {
            return R.error("删除错误");
        }

    }

    /**
     * 查询商品的三级分类
     * @return
     */
    @RequestMapping("/list/tree")
    public R listWithTree(){
        List<CategoryEntity> categoryList = categoryService.listWtihTree();
        return R.ok().put("data",categoryList);
    }

    /**
     * 修改分类
     */
    @RequestMapping("/update/sort")
    public R update(@RequestBody CategoryEntity[] category){

        categoryService.updateBatchById(Arrays.asList(category));
        return R.ok();
    }

}
